package com.ll.app.controller;

import com.ll.app.api.req.AddGoodsReqDTO;
import com.ll.app.api.req.AddShopReqDTO;
import com.ll.base.dto.ApiResultDTO;
import com.ll.base.dto.PageReqDTO;
import com.ll.domain.shop.biz.GoodsDomainService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;

@Slf4j
@RestController
@RequestMapping("/goods")
@RequiredArgsConstructor
@Api(tags = "商品API")
public class GoodsController {

    private final GoodsDomainService goodsDomainService;

    @ResponseBody
    @ApiOperation(value = "商品列表")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public ApiResultDTO page(@RequestBody @Validated PageReqDTO pageReq) {
        return ApiResultDTO.success(goodsDomainService.page(pageReq));
    }

    @ResponseBody
    @ApiOperation(value = "购买商品")
    @RequestMapping(value = "/byGoods", method = RequestMethod.POST)
    public ApiResultDTO byGoods(@RequestBody @Validated PageReqDTO pageReq) {
        return ApiResultDTO.success(goodsDomainService.page(pageReq));
    }

    @ResponseBody
    @ApiOperation(value = "添加商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", example = "whs", required = true),
            @ApiImplicitParam(name = "name", value = "名称", example = "文化衫", required = true),
            @ApiImplicitParam(name = "remarks", value = "说明", example = "说明"),
            @ApiImplicitParam(name = "creator", value = "创建者", example = "6015124", required = true),
            @ApiImplicitParam(name = "stock", value = "库存", example = "100", required = true),
            @ApiImplicitParam(name = "integral", value = "话费积分", example = "30", required = true),
            @ApiImplicitParam(name = "extData", value = "扩展属性"),
            @ApiImplicitParam(name = "file", value = "店铺图片", required = true, dataType = "__File")
    })
    @RequestMapping(value = "/addGoods", method = RequestMethod.POST)
    public ApiResultDTO addGoods(
            @RequestParam("code") String code,
            @RequestParam("name") String name,
            @RequestParam("remarks") String remarks,
            @RequestParam("creator") Long creator,
            @RequestParam("stock") Integer stock,
            @RequestParam("integral") Integer integral,
            @RequestParam("extData") String extData,
            @RequestParam("file") MultipartFile file) {

        AddGoodsReqDTO build = AddGoodsReqDTO.builder()
                .name(name)
                .remarks(remarks)
                .code(code)
                .extData(extData)
                .integral(integral)
                .stock(stock)
                .creator(creator)
                .build();
        goodsDomainService.add(build, file);
        return ApiResultDTO.success();
    }

}


